package org.game.gobang.client;

import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.game.constant.APIConstant;
import org.game.gobang.client.constant.ClientConstant;
import org.game.result.RestResult;
import org.game.result.RestResultBuilder;
import org.game.result.ResultCode;
import org.game.support.StopApp;
import org.game.util.MapUtil;
import org.game.util.YamlUtil;

import java.util.Map;
import java.util.concurrent.ThreadFactory;

public class GobangApp extends StopApp {
    private Thread gobangThread =
        new Thread(() -> {
            new GobangClient((int) APIConstant.CONFIG.get("port"), (String) APIConstant.CONFIG.get("ip")).start();

            RestResult restResult = new RestResultBuilder().success(ResultCode.ALL_ROOMS);
            ClientConstant.ctx.writeAndFlush(restResult);
        });

    @Override
    public void start(Stage primaryStage) throws Exception {
        Map<String, Object> config = YamlUtil.read("gobang\\gobang-client\\src\\main\\resources\\application.yml");

        MapUtil.cloneMap(APIConstant.CONFIG, config);

        gobangThread.start();

        Parent root = FXMLLoader.load(getClass().getResource("/fxml/gobang.fxml"));
        primaryStage.setTitle((String) config.get("title"));
        Scene scene = new Scene(root, (Integer) APIConstant.CONFIG.get("width") * (Integer) APIConstant.CONFIG.get("cellWidth") + 20,
                (Integer) APIConstant.CONFIG.get("height") * (Integer) APIConstant.CONFIG.get("cellHeight") + 120);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
